package ren.xbcr.schoolbus.client;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

public class SplashActivity extends AppCompatActivity {

    private final int SPLASH_DISPLAY_LENGTH = 3000;
    private TextView mCounterViewer;
    private boolean isAccessLocationPermission = false;
    private boolean isAccessStoragePermission = false;

    private static final int LOCATION_PERMISSION = 1;
    private static final int STORAGE_PERMISSION = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        mCounterViewer = (TextView) findViewById(R.id.splash_counter_viewer);


        new CountDownTimer(SPLASH_DISPLAY_LENGTH, 1000) {

            public void onTick(long millisUntilFinished) {
                mCounterViewer.setText(millisUntilFinished / 1000 + "");

            }

            public void onFinish() {

                grantPermission();
            }
        }.start();
/*
        *//* New Handler to start the Menu-Activity
         * and close this Splash-Screen after some seconds.*//*
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                *//* Create an Intent that will start the Menu-Activity. *//*
                Intent mainIntent = new Intent(SplashActivity.this, MainActivity.class);
                SplashActivity.this.startActivity(mainIntent);
                SplashActivity.this.finish();
            }
        }, SPLASH_DISPLAY_LENGTH);*/
    }

    private void grantPermission() {


        final String[] locationPermissions = new String[]{Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.ACCESS_COARSE_LOCATION};
        final String[] storagePermissions = new String[]{
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.READ_PHONE_STATE};

        if ((ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                || ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
                && !isAccessLocationPermission) {

            ActivityCompat.requestPermissions(this, locationPermissions, LOCATION_PERMISSION);
        } else {
            isAccessLocationPermission = true;

            if ((ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
                    || ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
                    && !isAccessStoragePermission) {

                ActivityCompat.requestPermissions(this, storagePermissions, STORAGE_PERMISSION);
            } else {
                isAccessStoragePermission = true;
            }
        }


        if (isAccessLocationPermission && isAccessStoragePermission) {

                /* Create an Intent that will start the Menu-Activity. */
            Intent mainIntent = new Intent(SplashActivity.this, MainActivity.class);
            mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            SplashActivity.this.startActivity(mainIntent);
            SplashActivity.this.finish();
        }


    }


    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {


        switch (requestCode) {
            case LOCATION_PERMISSION: {
                isAccessLocationPermission = true;
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    // permission was granted, yay! Do the
                    // contacts-related task you need to do.

                } else {

                    // permission denied, boo! Disable the
                    // functionality that depends on this permission.
                }
            }
            case STORAGE_PERMISSION: {
                isAccessStoragePermission = true;
            }

            // other 'case' lines to check for other
            // permissions this app might request
        }
        grantPermission();
    }

}
